/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package de.blitzcoder.collide.gui.fileexplorer;

import java.io.File;
import java.io.Serializable;
import javax.swing.event.TreeModelListener;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.TreeModel;
import javax.swing.tree.TreeNode;
import javax.swing.tree.TreePath;

/**
 *
 * @author blitzcoder
 */
public class FileTreeModel implements TreeModel,Serializable {

    private DefaultMutableTreeNode rootNode;
    
    public FileTreeModel() {
        rootNode = new DefaultMutableTreeNode("Dateisystem");
        
        // Add filesystem roots
        File[] roots = File.listRoots();
        for (File file:roots) {
            // A:\ and B:\ on windows systems are floppy drives. ignore them
            if (!file.equals(new File("A:\\")) && !file.equals(new File("B:\\")) )
                rootNode.add(new FileSystemNode(file));
        }
        
    }
    
    public Object getRoot() {
        return rootNode;
    }

    public Object getChild(Object parent, int index) {
        if (parent instanceof TreeNode) {
            TreeNode node = (TreeNode)parent;
            return node.getChildAt(index);
        } else {
            return "Error";
        }
    }

    public int getChildCount(Object parent) {
        if (parent instanceof TreeNode) {
            return ((TreeNode)parent).getChildCount();
        }
        return 0;
    }

    public boolean isLeaf(Object node) {
        if (node instanceof TreeNode) {
            return ((TreeNode)node).isLeaf();
        }
        return true;
    }

    public void valueForPathChanged(TreePath path, Object newValue) {
        System.out.println("1");
    }

    public int getIndexOfChild(Object parent, Object child) {
        if (parent instanceof TreeNode && child instanceof TreeNode) {
            ((TreeNode)parent).getIndex((TreeNode)child);
        }
        return -1;
    }

    // No listeners needed
    public void addTreeModelListener(TreeModelListener l) {}
    public void removeTreeModelListener(TreeModelListener l) {}



}
